package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.DeviceModuleStatus;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.exception.BusinessException;
import com.cloudfun.campusshare.common.model.dto.DeviceQueryDTO;
import com.cloudfun.campusshare.common.model.vo.*;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.DeviceEntity;
import com.cloudfun.campusshare.entity.UserEntity;
import com.cloudfun.campusshare.repo.DeviceEntityRepo;
import com.cloudfun.campusshare.repo.UserEntityRepo;
import com.cloudfun.campusshare.service.*;
import com.cloudfun.campusshare.util.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/v1/device")
@Api(value = "设备模块", description = "设备模块", position = 3)
@Validated
@Slf4j
@AuthRequired
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceEntityRepo deviceEntityRepo;

    @Autowired
    private DeviceOpServiceDelegate deviceOpService;

    @Autowired
    private InvokeHistoryService invokeHistoryService;

    @Autowired
    private UserEntityRepo userEntityRepo;

    @Autowired
    private VerifyCodeService verifyCodeService;

    @Autowired
    private DeviceCsqService deviceCsqService;

    @ApiOperation(value = "用户绑定设备", notes = "用户绑定设备")
    @RequestMapping(value = "/bindByUser", method = RequestMethod.POST)
    @AuthRequired
    BaseResult userBindDevice(@ApiParam(value = "DeviceVO", required = true) @Valid @RequestBody DeviceVO deviceVo) {
        return deviceService.bind(deviceVo);
    }

    @ApiOperation(value = "关联设备", notes = "关联设备")
    @PostMapping(value = "/relete")
    @AuthRequired
    BaseResult relete(@ApiParam(value = "设备关联信息", required = true) @Valid @RequestBody ReleteVO releteVo) {
        return deviceService.relete(releteVo);
    }

    @ApiOperation(value = "解除关联设备", notes = "解除关联设备")
    @GetMapping(value = "/unRelete")
    @AuthRequired
    BaseResult relete(@ApiParam(value = "洗衣机设备Imei")String washImei,@ApiParam(value = "加液器设备Imei")String pumpImei) {
        return deviceService.unRelete(washImei,pumpImei);
    }

    @ApiOperation(value = "用户解绑设备", notes = "用户解绑设备")
    @RequestMapping(value = "/unbindByUser", method = RequestMethod.GET)
    @AuthRequired
    BaseResult userUnbindDevice(@ApiParam(value = "设备id") @RequestParam(value = "deviceId") String imei) {
        BaseResult baseResult = deviceService.unbind(imei);
        return baseResult;
    }

    @ApiOperation(value = "用户批量解绑设备", notes = "用户批量解绑设备")
    @RequestMapping(value = "/unbindByUser/batch", method = RequestMethod.GET)
    @AuthRequired
    BaseResult userUnbindDevices(@ApiParam(value = "设备id集合") @RequestParam(value = "deviceIds", required = false) Set<String> imeiSet,
                                 @ApiParam(value = "关键字") @RequestParam(required = false) String value,
                                 @ApiParam(value = "所有") @RequestParam(required = false) Boolean unbindAll,
                                 @ApiParam(value = "用户id，管理员操作的话需要传") @RequestParam(required = false) String userId) {
        // 管理员操作删除所有必须传用户Id
        if (AuthenticationHolder.isAdmin() && StringUtils.isBlank(userId)) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        if (CollectionUtils.isEmpty(imeiSet) && StringUtils.isBlank(value) && BooleanUtils.isFalse(unbindAll)) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        if (!CollectionUtils.isEmpty(imeiSet)) {
            return deviceService.unbindAll(imeiSet, userId);
        } else if (StringUtils.isNotBlank(value)) {
            return deviceService.unbindAllByKeyword(value, userId);
        } else {
            return deviceService.unbindAll(userId);
        }
    }

    @ApiOperation(value = "查询设备列表", notes = "查询设备列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @AuthRequired
    BaseResult<PageResult<DeviceListVO>> queryEvidenceList(Pageable page, @ApiParam(value = "值") @RequestParam(value = "value", required = false) String value,
                                                           @ApiParam(value = "项目id") @RequestParam(value = "projectId", required = false) String projectId,
                                                           @ApiParam(value = "商户id") @RequestParam(value = "enterpriseId", required = false) String enterpriseId,
                                                           @ApiParam(value = "是否上线") @RequestParam(value = "online", required = false) Boolean online) {
        return deviceService.queryDeviceList(page, new DeviceQueryDTO(value, enterpriseId, projectId, online));
    }

    @GetMapping("/groupCount")
    @ApiOperation("分组统计设备")
    @AuthRequired(role = RoleType.ADMIN)
    BaseResult<PageResult<DeviceStatisticsVo>> groupCountByUser(Pageable pageable,
                                                                @ApiParam("搜索值") @RequestParam(value = "value", required = false) String value) {
        return deviceService.groupCountByUser(pageable, value);
    }

    @GetMapping("/count")
    @ApiOperation("统计设备")
    @AuthRequired
    BaseResult<?> countAll(@RequestParam(required = false) String userId) {
        return deviceService.countAll(userId);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("设备详情")
    @AuthRequired
    BaseResult<DeviceDetailVO> deviceDetail(@ApiParam(value = "设备id/设备Imei", required = true) @PathVariable(value = "id") String id) {
        DeviceDetailVO detail = deviceService.detail(id);
        return BaseResultFactory.produceResult(Code.SUCCESS, detail);
    }

    @GetMapping("/borrower/detail/{id}")
    @ApiOperation("金融机构、管理员查询商家设备详情")
    @AuthRequired(role = {RoleType.FINANCIAL_INSTITUTIONS, RoleType.ADMIN})
    BaseResult<BorrowerDeviceDetailVO> borrowerDeviceDetail(@ApiParam(value = "设备id", required = true) @PathVariable(value = "id") String id) {
        BorrowerDeviceDetailVO deviceDetailVo = deviceService.borrowerDeviceDetail(id);
        return BaseResultFactory.produceResult(Code.SUCCESS, deviceDetailVo);
    }

    @ApiOperation(value = "查询调用历史", notes = "查询调用历史")
    @RequestMapping(value = "/invokeHistoryList", method = RequestMethod.GET)
    @AuthRequired
    BaseResult<PageResult<InvokeHistoryVO>> invokeHistoryList(Pageable pageable, @ApiParam(value = "设备id", required = true) @RequestParam String id) {
        return BaseResultFactory.produceResult(Code.SUCCESS, invokeHistoryService.search(id, pageable));
    }

    @ApiOperation(value = "管理员查询商家设备列表", notes = "管理员查询商家设备列表")
    @RequestMapping(value = "/merchant/list", method = RequestMethod.GET)
    @AuthRequired(role = {RoleType.ADMIN,RoleType.OPERATIONAL_MERCHANT})
    BaseResult<PageResult<DeviceListVO>> queryMerchantEvidenceList(Pageable page, @Valid DeviceQueryDTO deviceQueryDTO) {
        return deviceService.queryDeviceList(page, deviceQueryDTO);
    }

    @ApiOperation(value = "金融机构查询商家设备列表")
    @RequestMapping(value = "/list/borrower-device", method = RequestMethod.GET)
    @AuthRequired(role = RoleType.FINANCIAL_INSTITUTIONS)
    BaseResult<PageResult<DeviceListVO>> queryBorrowerEvidenceList(Pageable page, @ApiParam(value = "商家Id") @RequestParam String borrowerId) {
        PageResult<DeviceListVO> deviceListVOPageResult = deviceService.queryBorrowerDeviceList(page, borrowerId);
        return BaseResultFactory.produceResult(Code.SUCCESS, deviceListVOPageResult);
    }

    @ApiOperation(value = "设备导出", notes = "设备导出")
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @AuthRequired(role = {RoleType.PLATFORM_MERCHANT, RoleType.OPERATIONAL_MERCHANT, RoleType.ADMIN})
    public BaseResult<?> exportExcel(HttpServletResponse response, @ApiParam(value = "设备id集合") @RequestParam(value = "deviceIds", required = false) Set<String> imeiSet,
                                     @ApiParam(value = "关键字") @RequestParam(required = false) String value,
                                     @ApiParam(value = "所有") @RequestParam(value = "unbindAll", required = false) Boolean all,
                                     @ApiParam(value = "用户id") @RequestParam(required = false) String userId) throws IOException {
        if (CollectionUtils.isEmpty(imeiSet) && StringUtils.isNotBlank(value) && BooleanUtils.isTrue(all)) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        long start = System.currentTimeMillis();
        List<DeviceExportVO> exportVOList;
        if (!CollectionUtils.isEmpty(imeiSet)) {
            exportVOList = deviceService.export(imeiSet, userId);
        } else if (StringUtils.isNotBlank(value)) {
            exportVOList = deviceService.exportByKeyword(value, userId);
        } else {
            exportVOList = deviceService.export(userId);
        }
        log.debug("导出{}个设备耗时：{}ms", exportVOList.size(), (System.currentTimeMillis() - start));
        UserEntity userEntity = userEntityRepo.findById(AuthenticationHolder.getUserId()).orElse(null);
        String username = userEntity.getUsername();
        ExcelUtils.exportExcel(exportVOList, null, "设备信息", DeviceExportVO.class, username + "-DeviceData", response);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @ApiOperation(value = "商家导出设备调用历史", notes = "商家导出设备调用历史")
    @RequestMapping(value = "/invokeHistory/export", method = RequestMethod.GET)
    @AuthRequired(role = {RoleType.PLATFORM_MERCHANT, RoleType.OPERATIONAL_MERCHANT})
    void invokeHistoryExport(HttpServletResponse response, @ApiParam(value = "设备id") @RequestParam("deviceId") String imei) throws IOException {
        long start = System.currentTimeMillis();
        List<InvokeHistoryMerchantExportVO> exportVOList = invokeHistoryService.merchantExportDeviceInvokeHistory(imei);
        log.debug("商家导出{}个设备使用记录耗时：{}ms", exportVOList.size(), (System.currentTimeMillis() - start));
        ExcelUtils.exportExcel(exportVOList, null, "调用历史信息", InvokeHistoryMerchantExportVO.class, imei + "-UsageRecord", response);
    }

    @ApiOperation(value = "金融机构、管理员导出设备调用历史", notes = "金融机构、管理员导出设备调用历史")
    @RequestMapping(value = "/borrower-device/invokeHistory/export", method = RequestMethod.GET)
    @AuthRequired(role = {RoleType.FINANCIAL_INSTITUTIONS, RoleType.ADMIN})
    void borrowerDeiceInvokeHistoryExport(HttpServletResponse response, @ApiParam(value = "设备id") @RequestParam("deviceId") String imei) throws IOException {
        long start = System.currentTimeMillis();
        List<InvokeHistoryFiAdminExportVO> exportVOList = invokeHistoryService.fiOrAdminExportDeviceInvokeHistory(imei);
        log.debug("金融机构、管理员导出{}个设备使用记录耗时：{}ms", exportVOList.size(), (System.currentTimeMillis() - start));
        DeviceEntity deviceEntity = deviceEntityRepo.findById(imei).orElseThrow(() -> new BusinessException(Code.NODATA));
        String userId = deviceEntity.getUserId();
        UserEntity userEntity = userEntityRepo.findById(userId).orElse(null);
        ExcelUtils.exportExcel(exportVOList, null, "调用历史信息", InvokeHistoryFiAdminExportVO.class, userEntity.getEnterpriseName() + "-" + imei + "-UsageRecord", response);
    }

    @ApiOperation(value = "金融机构导出商家设备")
    @PostMapping("/export/borrower-device-fi")
    @AuthRequired(role = RoleType.FINANCIAL_INSTITUTIONS)
    public void fiExportBorrowerDevice(HttpServletResponse response, @ApiParam(value = "商家Id") @RequestParam String borrowerId,
                                       @ApiParam(value = "项目id") @RequestParam(required = false) String projectId) throws Exception {
        long start = System.currentTimeMillis();
        List<BorrowerDeviceExportVO> borrowerDeviceExportVOList = deviceService.fiExportBorrowerDevice(borrowerId, projectId);
        log.debug("金融机构导出商家{}个设备耗时：{}ms", borrowerDeviceExportVOList.size(), (System.currentTimeMillis() - start));
        UserEntity lender = userEntityRepo.findById(AuthenticationHolder.getUserId()).orElse(null);
        UserEntity borrower = userEntityRepo.findById(borrowerId).orElse(null);
        ExcelUtils.exportExcel(borrowerDeviceExportVOList, null, "项目列表", BorrowerDeviceExportVO.class,
                borrower.getEnterpriseName() + "-" + lender.getEnterpriseName() + "-AssociatedDevices", response);
    }

    @ApiOperation(value = "管理员导出商家设备")
    @PostMapping("/export/borrower-device-admin")
    @AuthRequired(role = RoleType.ADMIN)
    public void adminExportBorrowerDevice(HttpServletResponse response, @ApiParam(value = "商家Id") @RequestParam String borrowerId,
                                          @ApiParam(value = "项目id") @RequestParam(required = false) String projectId) throws Exception {
        long start = System.currentTimeMillis();
        List<BorrowerDeviceExportVO> borrowerDeviceExportVOList = deviceService.adminExportBorrowerDevice(borrowerId, projectId);
        log.debug("管理员导出商家{}个设备耗时：{}ms", borrowerDeviceExportVOList.size(), (System.currentTimeMillis() - start));
        UserEntity borrower = userEntityRepo.findById(borrowerId).orElse(null);
        ExcelUtils.exportExcel(borrowerDeviceExportVOList, null, "项目列表", BorrowerDeviceExportVO.class,
                borrower.getEnterpriseName() + "-AssociatedDevices", response);
    }

    @ApiOperation(value = "客户获得设备信息")
    @PostMapping("/deviceInfo")
    @AuthRequired(role = RoleType.CUSTOMER)
    public BaseResult<?> getDeviceInfo(@ApiParam(value = "设备机器码") @RequestParam String machineNo) {
        return BaseResultFactory.produceResult(Code.SUCCESS, deviceService.getDeviceInfo(machineNo));
    }

    @ApiOperation(value = "根据洗衣点查询设备列表")
    @GetMapping("/list/byPoint")
    public BaseResult queryDeviceListByWashPoint(Pageable pageable, @ApiParam(value = "洗衣点id") @RequestParam String washPointId) {
        return BaseResultFactory.produceSuccess(deviceService.queryDeviceListByWashPoint(pageable, washPointId));
    }

    @ApiOperation(value = "运营平台查询设备列表")
    @GetMapping("/list/operPlat")
    public BaseResult operQueryDeviceList(Pageable pageable, @ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                          @ApiParam(value = "洗衣点id集合") @RequestParam(required = false) Set<String> washPointIds,
                                          @ApiParam(value = "设备类型") @RequestParam(required = false) String deviceType,
                                          @ApiParam(value = "设备型号") @RequestParam(required = false) String productType,
                                          @ApiParam(value = "设备状态") @RequestParam(required = false) Integer deviceStatus,
                                          @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
                                          @ApiParam(value = "省") @RequestParam(required = false) String province,
                                                      @ApiParam(value = "市") @RequestParam(required = false) String city) {
        return BaseResultFactory.produceSuccess(deviceService.queryDeviceListForOperPlat(pageable, washPointIds, keyword, deviceType, productType, deviceStatus, userId,province,city));

    }


    @ApiOperation(value = "运营平台导出设备")
    @GetMapping("/export/operPlat")
    public BaseResult operExportDevice(HttpServletResponse response, @ApiParam(value = "设备id集合") @RequestParam(value = "deviceIds", required = false) Set<String> ids,
                                       @ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                       @ApiParam(value = "设备类型") @RequestParam(required = false) String deviceType,
                                       @ApiParam(value = "设备状态") @RequestParam(required = false) Integer deviceStatus,
                                       @ApiParam(value = "是否导出所有") @RequestParam(required = false) Boolean all,
                                       @ApiParam(value = "用户id") @RequestParam(required = false) String userId,
                                       @ApiParam(value = "省") @RequestParam(required = false) String province,
                                       @ApiParam(value = "市") @RequestParam(required = false) String city) throws IOException {
        List<DeviceExportForOperVO> deviceExportForOperVOS = deviceService.exportDeviceForOperPlat(ids, keyword, deviceType, deviceStatus, all, userId);
        ExcelUtils.exportExcel(deviceExportForOperVOS, null, "设备信息", DeviceExportForOperVO.class, "设备信息", response);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "启用/停用设备")
    @PostMapping("/switch/operPlat")
    public BaseResult startOrEndingUsingDevice(@ApiParam(value = "设备id") @RequestParam("deviceId") String id, @ApiParam(value = "是否启用", required = true) @RequestParam Boolean isUsing) {
        deviceService.startOrEndingDevice(id, isUsing);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "批量启用/停用设备")
    @PostMapping("/batch/switch/operPlat")
    public BaseResult batchStartOrEndingUsingDevice(@RequestBody BatchSwitchDeviceVO batchSwitchDeviceVO) {
        deviceService.batchStartOrEndingDevice(batchSwitchDeviceVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "运营平台查询设备详情")
    @GetMapping("/detail/operPlat/{id}")
    public BaseResult operQueryDeviceDetail(@PathVariable String id) {
        return BaseResultFactory.produceSuccess(deviceService.operQueryDeviceDetail(id));
    }

    @ApiOperation(value = "运营平台查询设备调用历史记录")
    @GetMapping("/invokeHistory/operPlat/{id}")
    public BaseResult operQueryUseHistory(Pageable pageable, @PathVariable String id) {
        return BaseResultFactory.produceSuccess(deviceService.operQueryInvokeHistory(pageable, id));
    }

    @ApiOperation(value = "运营平台编辑设备")
    @PostMapping("/edit/operPlat")
    public BaseResult operEditDevice(@RequestBody EditDeviceForOperVO editDeviceForOperVO) {
        deviceService.operEditDevice(editDeviceForOperVO);
        return BaseResultFactory.produceSuccess();
    }

    // 首页将会调用
    @ApiOperation(value = "运营平台设备状态统计")
    @PostMapping("/om/deviceStateStat")
    @AuthRequired(role = {RoleType.OPERATIONAL_MERCHANT,RoleType.ADMIN})
    public BaseResult<OMDeviceStateStatisticsVO> omDeviceStateStatistics(@ApiParam(value = "设备种类") @RequestParam(required = false) String productType,
                                                                         @ApiParam(value = "设备种类") @RequestParam(required = false) String deviceType,
                                                                         @ApiParam(value = "常用洗衣点") @RequestParam(required = false) String preferWashPointId) {
        OMDeviceStateStatisticsVO deviceStateStatisticsVO = deviceService.omDeviceStateStatistics(productType, deviceType, preferWashPointId);
        return BaseResultFactory.produceSuccess(deviceStateStatisticsVO);
    }

    @ApiOperation(value = "批量更改洗衣点")
    @PostMapping("/batchChange/washPoint")
    public BaseResult batchChangeWashPoint(@RequestBody BatchEditDeviceVO batchEditDeviceVO) {
        deviceService.batchChangeWashPoint(batchEditDeviceVO.getIds(), batchEditDeviceVO.getBusinessId());
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "批量更改设备收费模式")
    @PostMapping("/batchChange/deviceMode")
    public BaseResult batchChangeDeviceMode(@RequestBody BatchEditDeviceVO batchEditDeviceVO) {
        deviceService.batchChangeDeviceMode(batchEditDeviceVO.getIds(), batchEditDeviceVO.getBusinessId());
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "批量更改设备服务时间")
    @PostMapping("/batchChange/serviceTime")
    public BaseResult batchChangeServiceTime(@RequestBody BatchEditDeviceVO batchEditDeviceVO) {
        deviceService.batchChangeServiceTime(batchEditDeviceVO.getIds(), batchEditDeviceVO.getBusinessId());
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "批量解绑（小程序）")
    @PostMapping("/batch/unbind")
    public BaseResult batchUnbindDevice(@RequestBody BatchEditDeviceVO batchEditDeviceVO) {
        return BaseResultFactory.produceSuccess(deviceService.batchUnbindDeviceForDevOps(batchEditDeviceVO.getIds()));
    }

    @ApiOperation(value = "添加设备")
    @PostMapping("/add")
    public BaseResult addDevice(@Valid @RequestBody DeviceAddForChargeVO deviceAddForChargeVO) {
        return deviceService.addDeviceForDevOps(deviceAddForChargeVO);
    }

    @ApiOperation(value = "判断设备是否已被绑定")
    @GetMapping("/bind/judge")
    public BaseResult judgeDevice(@ApiParam(value = "机器码", required = false) @RequestParam(required = false) String machineNo,
                                  @ApiParam(value = "设备识别码", required = false) @RequestParam(required = false) String imei) {
        return BaseResultFactory.produceSuccess(deviceService.judgeDevice(machineNo, imei));
    }

    @ApiOperation(value = "用户复位操作", notes = "用户复位操作")
    @RequestMapping(value = "/resetByUser", method = RequestMethod.POST)
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult resetByUser(@ApiParam(value = "deviceResetVo", required = true) @Valid @RequestBody DeviceResetVO deviceResetVo) {
        return deviceOpService.reset(deviceResetVo.getImei());
    }

    @ApiOperation(value = "产线测试，搜索设备")
    @GetMapping("/line-test/search")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> searchLineTestDeviceList(@ApiParam(value = "设备识别码") @RequestParam(required = false) String imei,
                                                  @ApiParam(value = "模块状态") @RequestParam(required = false) DeviceModuleStatus moduleStatus,
                                                  @ApiParam(value = "二维码") @RequestParam(required = false) String qrCode, Pageable pageable) {
        return BaseResultFactory.produceResult(Code.SUCCESS, deviceService.searchLineTestDeviceList(imei, moduleStatus, qrCode, pageable));
    }

    @ApiOperation(value = "产线测试，给设备发送验证码")
    @GetMapping("/line-test/deviceCode")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> sendDeviceCode(@ApiParam(value = "设备id") @RequestParam String deviceId) {
        return verifyCodeService.deviceCodeCreate(deviceId);
    }

    @ApiOperation(value = "产线测试，验证设备验证码")
    @PostMapping("/line-test/deviceCode")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> verifyDeviceCode(@ApiParam(value = "设备id") @RequestParam String deviceId,
                                          @ApiParam(value = "验证码") @RequestParam String code) {
        return deviceService.lineTestDevice(deviceId, code);
    }

    @ApiOperation(value = "出厂测试，导入设备")
    @PostMapping("/factory-test/import")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult<?> deviceImportForFactory(MultipartFile file) {
        if (file.isEmpty()) {
            return BaseResultFactory.produceResult(Code.ERROR, "文件为空");
        }
        return deviceService.deviceImportForFactory(file);
    }

    @ApiOperation(value = "查询设备信号强度列表")
    @GetMapping("/csq/list")
    @AuthRequired(role = RoleType.ADMIN)
    public BaseResult queryDeviceCsqList(Pageable pageable, @ApiParam(value = "imei") @RequestParam(required = false) String imei) {
        return BaseResultFactory.produceSuccess(deviceCsqService.queryByImei(imei, pageable));
    }
}
